using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web;
using System.Xml;
using System;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		First Surname	
	Started:	yyyy-mm-dd	
	Status:		dev/alpha/beta/release	
	Version:	X.x
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX.workflowX {
	/// <summary>
	/// Description of the x_workreport class.
	/// <para>Additional information about the class</para>
	/// </summary>
	public class x_workreport : x_DbData {
		#region Constants
		private const string workreportxml = "<workflowX><workreport/></workflowX>";
		private const string error_ 	= "An error message.";
		private const string comment_	= "A comment.";
		private const string message_ 	= "A message.";
		#endregion

		#region Invisible properties
		private XmlDocument workreport;
		#endregion

		#region Visible properties
		private string property;
		/// <summary>Definiton of the property</summary>
		/// <value>Description of the value of the property</value>
		public string Property {
			get {
				return property;
			}
			set {
				property = value;
			}
		}
		/// <summary>The definition of the work-report</summary>
		/// <value>An Xml Element that describes the work-report</value>
		public XmlElement Report {
			get { return (XmlElement) workreport.SelectSingleNode("/workflowX/workreport"); }
		}
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		/// <param name="DSN">the dsn (from web.config)</param>
		public x_workreport(string DSN) : base(DSN) {
			initialise();
		}
		/// <summary>Constructor description</summary>
		/// <param name="prop">Brief description of the parameter</param>
		/// <param name="DSN">the dsn (from web.config)</param>
		public x_workreport(string DSN, string prop) : base(DSN) {
			property = prop;
			initialise();
		}
		#endregion

		#region Web Service methods
		/// <overloads>Retreives a work-report from the database</overloads>
		///	<summary>
		///	The work-report is identified
		///	</summary>
		/// <param name="ownerID">The process Owner's identifier, '0' if all</param>
		/// <param name="statusID">Reported work work-status identifier, '0' if all</param>
		/// <param name="datetype">Reported work period, 1,2 or 3 for week, month or year</param>
		/// <param name="dateval">Number of work periods from current date</param>
		/// <exception cref="x_exception">Cannot find a work-report for the given identifier</exception>
		public void GetWorkReport(int ownerID, int statusID, int datetype, int dateval) {
			Report.SetAttribute("ownerID", ownerID.ToString());
			Report.SetAttribute("statusID", statusID.ToString());
			Report.SetAttribute("datetype", datetype.ToString());
			Report.SetAttribute("dateval", dateval.ToString());
			getWorkReport(ownerID, statusID, datetype, dateval);
		}
		private void getWorkReport(int ownerID, int statusID, int datetype, int dateval) {
			_getCommand("xReportOwnerDate", CommandType.StoredProcedure);
			_addDbParameter(ownerID, "@OwnerID");
			_addDbParameter(statusID, "@StatusID");
			_addDbParameter(datetype, "@Datetype");
			_addDbParameter(dateval, "@Dateval");
			SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
			load(rdr);
			rdr.Close();
		}
		#endregion

		#region Public methods
		#endregion
		
		#region Protected methods
		#endregion
				
		#region Private methods
		/// <summary>
		/// Initialise the class (called from constructors)
		/// </summary>
		private void initialise() {
			workreport = new XmlDocument();
			workreport.LoadXml(workreportxml);
		}
		/// <summary>
		/// Loads the current work-report from the datastore using the supplied datareader
		/// </summary>
		private void load(SqlDataReader rdr) {
			x_workflow workflowx = new x_workflow();
			while (rdr.Read()) {
				XmlElement thisitem = workreport.CreateElement("reportitem");
				// ProcessID, AgentID, Total, Normal, Escalated, Urgent, Completed, Inprogress, Onhold, Stalled, Cancelled
				_addAttribute(workreport, thisitem, "processID", _getString(rdr, "ProcessID"));
				_addAttribute(workreport, thisitem, "process", _getString(rdr, "Description"));
				_addAttribute(workreport, thisitem, "agentID", _getString(rdr, "AgentID"));
				_addAttribute(workreport, thisitem, "total", _getString(rdr, "Total"));
				_addAttribute(workreport, thisitem, "normal", _getString(rdr, "Normal"));
				_addAttribute(workreport, thisitem, "escalated", _getString(rdr, "Escalated"));
				_addAttribute(workreport, thisitem, "urgent", _getString(rdr, "Urgent"));
				_addAttribute(workreport, thisitem, "completed", _getString(rdr, "Completed"));
				_addAttribute(workreport, thisitem, "inprogress", _getString(rdr, "Inprogress"));
				_addAttribute(workreport, thisitem, "onhold", _getString(rdr, "Onhold"));
				_addAttribute(workreport, thisitem, "stalled", _getString(rdr, "Stalled"));
				_addAttribute(workreport, thisitem, "cancelled", _getString(rdr, "Cancelled"));
				try {
					XmlElement gkAgent = workflowx.GetUser(_getString(rdr, "AgentID"));
					_addAttribute(workreport, thisitem, "agent", String.Concat(gkAgent.SelectSingleNode("FirstName").InnerText, " ", gkAgent.SelectSingleNode("Surname").InnerText));
				} catch(x_exception) {
					_addAttribute(workreport, thisitem, "agent", "[unknown]");
				}
				Report.AppendChild(thisitem);
			}
		}
		#endregion
	}
}
